package com.example.mansystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mansystem.dto.ChangeProcessDto;
import com.example.mansystem.entity.CallResultMsg;
import com.example.mansystem.entity.ProcessMangerDO;
import com.example.mansystem.exception.CodeAndMsg;
import com.example.mansystem.exception.UniformReponseHandler;
import com.example.mansystem.exception.UserDefinedException;
import com.example.mansystem.mapper.ProcessMangerMapper;
import com.example.mansystem.service.ProcessImageService;
import com.example.mansystem.vo.InPresentVO;
import com.example.mansystem.vo.ProcessManagerVO;
import org.activiti.engine.RuntimeService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author Tethamo
 */
@RestController
@RequestMapping("/activiti")
public class ProcessImageApi {

    ProcessImageService processImageService;

    @Resource
    ProcessMangerMapper processMangerMapper;

    @Autowired
    public ProcessImageApi(ProcessImageService processImageService) {
        this.processImageService = processImageService;
    }



    /**
     * 生成流程图
     * @param processInstanceId 流程实例ID
     * @param httpServletResponse response entity
     */
    @GetMapping("/{processInstanceId}")
    public void processImageGet(@PathVariable String processInstanceId, HttpServletResponse httpServletResponse){
        if (StringUtils.isEmpty(processInstanceId)) {
            throw new UserDefinedException(CodeAndMsg.UNKNOWEXCEPTION);
        }
        try {
            InputStream img = processImageService.getFlowImgByProcInstId(processInstanceId);
            byte[] bytes = IOUtils.toByteArray(img);
            httpServletResponse.setContentType("image/svg+xml");
            OutputStream outputStream = httpServletResponse.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            throw new UserDefinedException(CodeAndMsg.UNKNOWEXCEPTION);
        }
    }

    @GetMapping("/process/{processKey}")
    public void getImgByProcessDefinitionKey(@PathVariable String processKey, HttpServletResponse httpServletResponse){
        if (StringUtils.isEmpty(processKey)) {
            throw new UserDefinedException(CodeAndMsg.UNKNOWEXCEPTION);
        }
        try {
            InputStream img = processImageService.getImgByProcessDefinitionKey(processKey);
            byte[] bytes = IOUtils.toByteArray(img);
            httpServletResponse.setContentType("image/svg+xml");
            OutputStream outputStream = httpServletResponse.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            throw new UserDefinedException(CodeAndMsg.UNKNOWEXCEPTION);
        }
    }

    @GetMapping("/getProcess")
    public CallResultMsg getProcess()
    {
        List<ProcessMangerDO> list = processMangerMapper.selectList(null);
        ProcessManagerVO res= ProcessManagerVO.builder().processMangerDOList(list).build();
        return new UniformReponseHandler<ProcessManagerVO>().sendSuccessResponse(res);
    }

    @PostMapping("/changeProcess")
    public CallResultMsg changeProcess(@RequestBody ChangeProcessDto dto)
    {
        LambdaQueryWrapper<ProcessMangerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessMangerDO::getId,dto.getId());
        ProcessMangerDO processMangerDO = processMangerMapper.selectOne(wrapper);
        if(dto.getType()==1)
        {
            int type=processMangerDO.getType();
            wrapper.clear();
            wrapper.eq(ProcessMangerDO::getType,type);
            List<ProcessMangerDO> processMangerDOList = processMangerMapper.selectList(wrapper);
            for(int i=0;i<processMangerDOList.size();i++)
            {
                processMangerDOList.get(i).setState(0);
                processMangerMapper.updateById(processMangerDOList.get(i));
            }
            processMangerDO.setState(1);
            processMangerMapper.updateById(processMangerDO);
        }
        else {
            processMangerDO.setState(0);
            processMangerMapper.updateById(processMangerDO);
        }
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }
}